import json

from flask import Blueprint, redirect, render_template, request, url_for

from src.CarsShop import CarsShop
from src.DBStrategy import FileDBStrategy, SQLiteDBStrategy
from src.IOStrategy import FlaskIOStrategy, JSONIOStrategy

bp = Blueprint("st0713", __name__)
sql_path = "./shop.sqlite"
path = "aam2307/st13/"
show_endp = "st0713.show_vehicles"
parent_url = ""

FlaskIOStrategy = FlaskIOStrategy()
File_strategy = FileDBStrategy()
JSON_Strategy = JSONIOStrategy()
DB_strategy = SQLiteDBStrategy(sql_path)
Shop = CarsShop(FlaskIOStrategy, File_strategy, DB_strategy)


# -----------------------API--------------------------------------


@bp.route("/api/save", methods=["POST"])
def apiSave():
    Shop.io_strategy = JSON_Strategy
    Shop.clear_all()
    print(request.json)
    j = json.loads(request.json)
    print(j)
    for car in j:
        JSON_Strategy.set_car(car)
        Shop.create_any_vehicle(type_of_c=car["type"])
    return json.dumps("Ok")


@bp.route("/api/saveOne", methods=["POST"])
def apiSaveOne():
    Shop.io_strategy = JSON_Strategy
    # Shop.clear_all()
    print(request.json)
    try:
        j = json.loads(request.json)
    except:
        j = request.json
    print(j)

    Shop.delete_car(j["id"])
    JSON_Strategy.set_car(j)
    Shop.create_any_vehicle_with_inv(j["type"], j["id"])
    return json.dumps("Ok")


@bp.route("/api/clear", methods=["POST"])
def apiClear():
    Shop.io_strategy = JSON_Strategy
    Shop.clear_all()
    return json.dumps("Ok")


@bp.route("/api/load", methods=["POST"])
def apiLoad():
    Shop.io_strategy = JSON_Strategy
    c = Shop.show_catalog()
    return JSON_Strategy.catalog_to_json(c)


@bp.route("/api/delete/<int:inventory_number>", methods=["POST"])
def confirm_delete_vehcle_api(inventory_number):
    Shop.io_strategy = FlaskIOStrategy
    Shop.delete_car(inventory_number)
    return json.dumps("Ok")


# ----------------------WEB---------------------------------------


@bp.route("/")
def index():
    Shop.io_strategy = FlaskIOStrategy
    global parent_url
    parent_url = "/" + request.url_rule.rule.split("/")[1]
    if not Shop.initial_load:
        Shop.update_buffer(parent_url)
    return render_template(path + "index.html")


@bp.route("/create_car", methods=["GET"])
def create_car():
    Shop.io_strategy = FlaskIOStrategy
    return render_template(path + "create_vehicle.html", type="car", url=parent_url)


@bp.route("/create_car", methods=["POST"])
def add_car():
    Shop.io_strategy = FlaskIOStrategy
    Shop.create_any_vehicle(type_of_c="car")
    return redirect(url_for(show_endp))


@bp.route("/create_truck", methods=["GET"])
def create_truck():
    Shop.io_strategy = FlaskIOStrategy
    return render_template(path + "create_vehicle.html", type="truck", url=parent_url)


@bp.route("/create_truck", methods=["POST"])
def add_truck():
    Shop.io_strategy = FlaskIOStrategy
    Shop.create_any_vehicle(type_of_c="truck")
    return redirect(url_for(show_endp))


@bp.route("/show_vehicles", methods=["GET"])
def show_vehicles():
    Shop.io_strategy = FlaskIOStrategy
    return render_template(
        path + "list.html", journal=Shop.show_catalog(), url=parent_url
    )


@bp.route("/edit_vehicle/<int:inventory_number>", methods=["GET"])
def edit_vehicle(inventory_number):
    Shop.io_strategy = FlaskIOStrategy
    return render_template(
        path + "edit_vehicle.html",
        vehicle_to_edit=Shop.get_by_number(inventory_number),
        type=Shop.get_by_number(inventory_number).type,
        num=inventory_number,
        url=parent_url,
    )


@bp.route("/edit_vehicle/<int:inventory_number>", methods=["POST"])
def confirm_edit_vehicle(inventory_number):
    Shop.io_strategy = FlaskIOStrategy
    Shop.edit_car(inventory_number)
    return redirect(url_for(show_endp))


@bp.route("/del_vehicle/<int:inventory_number>", methods=["POST", "GET"])
def confirm_delete_vehcle(inventory_number):
    Shop.io_strategy = FlaskIOStrategy
    Shop.delete_car(inventory_number)
    return redirect(url_for(show_endp))


@bp.route("/save")
def save():
    Shop.io_strategy = FlaskIOStrategy
    Shop.save()
    return render_template(path + "success.html", url=parent_url)


@bp.route("/load")
def load():
    Shop.io_strategy = FlaskIOStrategy
    Shop.load()
    return redirect(url_for(show_endp))


@bp.route("/clear")
def clear():
    Shop.io_strategy = FlaskIOStrategy
    Shop.clear_all()
    return redirect(url_for(show_endp))
